var config = require('../../../utils/config.js');
var netutil = require('../../../utils/netutil.js');
var baseutil = require('../../../utils/baseutil.js');
let { WeToast } = require('../../../src/wetoast.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    DataUserInfo: "",
    selectUserID: "",
    selectCount: 0,
    selectDataUserInfo: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var DataUserInfo = app.globalData.tempData
    app.globalData.tempData = null
    var selectDataUserInfo = app.globalData.tempData2
    app.globalData.tempData2 = null
    if (DataUserInfo == null || DataUserInfo.length == 0) {
      this.setData({ DataUserInfo: null, })
    } else {
      this.setData({
        DataUserInfo: DataUserInfo,
        selectUserID: options.selectUserID,
        selectCount: options.selectUserID.split("-").length,
        selectDataUserInfo: selectDataUserInfo
      })

    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  checkboxChange: function (e) {
    var position = parseInt(e.currentTarget.dataset.position)
    var userInfo = this.data.DataUserInfo[position]
    var selectUserID = this.data.selectUserID
    if (baseutil.isContains(selectUserID, userInfo.UserID)) {
      selectUserID = selectUserID.replace("-" + userInfo.UserID, '').replace(userInfo.UserID, '')
    } else {
      if (selectUserID != null && selectUserID.length > 0)
        selectUserID += "-" + userInfo.UserID
      else
        selectUserID += + userInfo.UserID
    }
    if (selectUserID.length > 0 && selectUserID.substring(0, 1) == '-') {
      selectUserID = selectUserID.substring(1, selectUserID.length)
    }

    var selectCount = 0;
    if (selectUserID.length > 0) {
      selectCount = selectUserID.split("-").length
    }
    this.setData({ selectUserID: selectUserID, selectCount: selectCount })
    baseutil.log("+++" + selectUserID)
  },
  confirm: function () {
    var selectCount = this.data.selectCount
    if (selectCount == 0) {
      return;
    }
    var selectUserID = this.data.selectUserID
    var DataUserInfo = this.data.DataUserInfo

    var tempDataUserInfo = null
    baseutil.log("this.data.selectDataUserInfo=")
    baseutil.log(this.data.selectDataUserInfo)




    if (this.data.selectDataUserInfo == null || this.data.selectDataUserInfo.length == 0) {
      tempDataUserInfo = new Array();
    } else {
      tempDataUserInfo = this.data.selectDataUserInfo
      baseutil.log(selectDataUserInfo)
      baseutil.log("=selectDataUserInfo")

    }
    var lastSelectUserID = ''
    for (var i = 0; i < tempDataUserInfo.length; i++) {
      lastSelectUserID += tempDataUserInfo[i].UserID
    }
    baseutil.log(lastSelectUserID + "------------" + selectUserID)
    var selectArray = selectUserID.split("-")
    var lastSelectArray = lastSelectUserID.split("-")
    baseutil.log(selectArray)
    baseutil.log(lastSelectArray)
    for (var i = 0; i < DataUserInfo.length; i++) {//添加已选择的

      if (baseutil.arrayContain(selectArray, DataUserInfo[i].UserID)) {
        if (!baseutil.arrayContain(lastSelectArray, DataUserInfo[i].UserID)) {
          baseutil.log((selectArray.indexOf(DataUserInfo[i].UserID)) + "---" + (!lastSelectArray.indexOf(DataUserInfo[i].UserID)))
          baseutil.log(selectUserID + "----" + DataUserInfo[i].UserID + "----" + selectArray.indexOf(DataUserInfo[i].UserID))
          tempDataUserInfo.push(DataUserInfo[i])
          baseutil.log(DataUserInfo[i])
        }


      }
    }



    for (var i = 0; i < DataUserInfo.length; i++) {//取出之前选择  现在取消的
      if (!baseutil.isContains(selectUserID, DataUserInfo[i].UserID) && baseutil.isContains(lastSelectUserID, DataUserInfo[i].UserID)) {
        for (var j = 0; j < tempDataUserInfo.length; j++) {
          if (DataUserInfo[i].UserID == tempDataUserInfo[j].UserID) {
            tempDataUserInfo[j] = null
          }

        }

      }
    }
    baseutil.log(tempDataUserInfo)
    baseutil.log("-----a")
    var tempData = tempDataUserInfo;
    var tempDataUserInfo = new Array();
    for (var j = 0; j < tempData.length; j++) {
      if (tempData[j] != null) {
        tempDataUserInfo.push(tempData[j])
      }

    }
    baseutil.log(tempDataUserInfo)
    baseutil.log("-----b")
    var pages = getCurrentPages();
    var prePage = pages[pages.length - 2];  //上一个页面
    prePage.updateData();

    var pages = getCurrentPages();
    var prePage = pages[pages.length - 3];  //上两个页面
    prePage.updateApproveData(tempDataUserInfo);
    baseutil.log(tempDataUserInfo)
    baseutil.log("-----c")
    baseutil.returnLastTwoPage(2);
  },
  getSearchText(e) {
    var searchText = e.detail.value;
    baseutil.log(searchText)

    this.setData({
      searchText: searchText
    })
  },
  searchApprover() {

  },

})